package arithmetic.LeetCode;

import utils.TreeNode;

/**
 * @author jiangfeng on 2023/6/13
 */
public class IsSymmetric {
    public static void main(String[] args) {
        System.out.println(new Solution().isSymmetric(TreeNode.createTree(1,2,2,3,4,4,3)));
    }
    static class Solution {
        public boolean isSymmetric(TreeNode root) {
            //把左边翻转一下
            if(root.left!=root.right){
                return false;
            }
            reverse(root.right);
            boolean res = isSamle(root.left,root.right);

            reverse(root.right);
            return res;
        }
        public boolean isSamle(TreeNode r1,TreeNode r2){
            if(r1==null && r2==null){
                return true;
            }
            if(r1.val!=r2.val){
                return false;
            }
            return isSamle(r1.left,r2.left)&&isSamle(r1.right,r2.right);
        }

        public TreeNode reverse(TreeNode root){
            if(root!=null){
                reverse(root.left);
                reverse(root.right);
                TreeNode temp = root.right;
                root.right=root.left;
                root.left=temp;
            }
            return root;
        }
    }
}
